<?php
/**
* queen文件
* ==============================================
* 版权所有 
* ----------------------------------------------
* 这不是一个自由软件，未经授权不许任何使用和传播。
* ==============================================
* @date: 2016-12-18
* @author: yqh
* @version:
*/
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    
    public function __construct(){
        parent::__construct();
        if (!$_SESSION['index_Info']) {
            header("Location:./index.php?s=/Home/Check/login&from=index");
        }
        $this->assign('loginName',$_SESSION['index_Info']['uname']);
        $this->assign('account',$_SESSION['index_Info']);
    }
    
    public function index(){
    	$this->display();
    }
    
    /**
    * 函数用途描述: 选择试卷
    * @date: 2016-12-27上午10:26:46
    * @author: yqh
    * @return:array $list
    */
    public function choosePaper() {
    	$paper = M("paper");
    	
    	$list = $paper->where(['status' => 1])->field('id,name,description')->select();
    	$this->assign('list',$list);
    	$this->display();
    }
    
    /**
    * 函数用途描述: 答题页面
    * @date: 2016-12-27上午10:27:27
    * @author: yqh
    * @param:param
    * @return:
    */
    public function exam() {
    	$paperId = I("get.paperId");
	    if ($paperId) {
				try {
					$paper = M("paper");
					$sub = M("subject");
					$field = M("field");
					$record = M("record");
					$user_id = $_SESSION['index_Info']['id'];
					$list = $record->where(['user_id' => $user_id,'paper_id' => $paperId])->count();
					if ($list > 0) {
						header("Location: /index.php?s=Home/index/choosePaper&error=该问卷已答过，不可重复答题");
					}
					//试卷信息
					$pinfo = $paper->where(['id' => $paperId,'status' => 1])->field('id,name,description')->find();
					$result = array();
					if (count($pinfo) > 0) {
						$result['paper_info'] = $pinfo;
						//试卷的题目信息
						$subinfo = $sub->where(['paper_id' => $paperId,'status' => 1])->select();
						if (count($subinfo) > 0) {
							foreach ($subinfo as $k => $v) {
								$con['paper_id'] = $paperId;
								$con['subject_id'] = $v['id'];
								$con['status'] = 1;
								$finfo = $field->where($con)->field('id,paper_id,subject_id,title,correct')->select();
								$subinfo[$k]['field'] = $finfo;
							}
							$result['subject_info'] = $subinfo;
						}
					}
				} catch (\Exception $e) {
					throw $e;
				}
			}
		$this->assign("info",$result);
    	$this->display();
    }
    
    /**
    * 函数用途描述: 保存用户答题记录
    * @date: 2016-12-27下午4:30:09
    * @author: yqh
    */
    public function saveRecord() {
    	$data = I("post.");
    	try {
    		$record = M("record");
    		$user_id = $_SESSION['index_Info']['id'];
    		$list = $record->where(['user_id' => $user_id,'paper_id' => $data['paper_id']])->count();
    		if ($list > 0) {
    			$this->ajaxReturn(-1);
    		}
    		$array = array('user_id' => $user_id,
    					'paper_id' => $data['paper_id'],
    					'score' => $data['score'],
    					'answer_info' => serialize($data['record']),
    					'created_time' => date("Y-m-d H:i:s",time()),
    					'updated_time' => date("Y-m-d H:i:s",time())
    		);
    		$result = $record->add($array);
    		$this->ajaxReturn($result);
    	} catch (\Exception $e) {
    		throw $e;
    	}
    }
    
    /**
    * 函数用途描述
    * @date: 2016-12-27 下午9:08:03
    * @author: yqh
    * @param: variable
    * @return:
    */
    public function account() {
        try {
            $obj = M("record");
            $record = $obj->where(['user_id' => $_SESSION['index_Info']['id']])->field('paper_id,score')->select();
            $this->assign('record',$record);
        } catch (\Exception $e) {
            throw $e;
        }
        $this->display();
    }
    
    /**
    * 函数用途描述: 保存个人基本信息
    * @date: 2016-12-29上午9:46:36
    * @author: yqh
    * @param:array $data
    */
    public function saveBase(){
    	$data = I("post.");
    	try {
    		$user = M("user");
    		$array = array ('number' => $data['number'],
    				'gender' => $data['gender'],
    				'phone' => $data['phone'],
    				'email' => $data['email'],
    				'updated_time' => date('Y-m-d H:i:s',time())
    		);
    		$result = $user->where(['id' => $_SESSION['index_Info']['id']])->save($array);
    		if ($result) {
    			$_SESSION['index_Info'] = $user->where(['id' => $_SESSION['index_Info']['id']])->find();
    		}
    		header("Location:/index.php?s=Home/index/account");
    	} catch (\Exception $e) {
    		throw $e;
    	}
    }
    
    /**
    * 函数用途描述: 
    * @date: 2016-12-29上午10:20:06
    * @author: yqh
    * @param:param
    * @return:
    */
    public function results() {
    	try {
    		$curPage = I("post.page");
    		$post = $curPage;
    		$per = 10;//每页条数
    		$curPage = $curPage ? $curPage : 1;
    		$record = M('record a');
    		//多表查询（方法一）
    		$result['info'] = $record
    				->join('LEFT JOIN ex_paper b ON a.paper_id = b.id')
    				->field('a.score,a.created_time,b.id,b.name')
    				->where('a.user_id = %d',array($_SESSION['index_Info']['id']))
    				->limit(($curPage-1)*$per,$per)
    				->select();
    		//多表查询（方法二）
//     		$result = M()->table(array('ex_record' => 'a','ex_paper' => 'b'))->field('a.score,a.created_time,b.name')->where('a.user_id = %d  and a.paper_id = b.id',array($_SESSION['index_Info']['id']))->select();
    		$num = $record->where(['user_id' => $_SESSION['index_Info']['id']])->count();
    		$page = ceil($num/$per);
    		if (count($result['info']) > 0) {
    			$result['num'] = $num;
    			$result['page'] = $page;
    			$result['curPage'] = $curPage;
    			$result['prePage'] = $curPage != 1 ? $curPage-1 :1;
    			$result['nextPage'] = $curPage != $page ? $curPage+1 : $page;
    		}
    		if ($post) {
    			if (count($result['info']) > 0) {
    				$this->ajaxReturn($result);
    			} else {
    				$this->ajaxReturn(0);
    			}
    		}
    		$this->assign('result',$result ? $result : '');
    	} catch (\Exception $e) {
    		throw $e;
    	}
    	$this->display();
    }
    
    /**
    * 函数用途描述: 
    * @date: 2017-1-4下午2:04:35
    * @author: yqh
    * @param:param
    * @return:
    */
    public function details() {
    	$paperId = I("get.paperId");
    	if ($paperId) {
	    	try {
	    		$paper = M("paper");
	    		$sub = M("subject");
	    		$field = M("field");
	    		$record = M("record");
	    		$user_id = $_SESSION['index_Info']['id'];
	    		$result = array();
	    		//试卷信息
	    		$pinfo = $paper->where(['id' => $paperId,'status' => 1])->field('id,name,description')->find();
	    		$aInfo = $record->where(['user_id' => $user_id,'paper_id' => $paperId])->field('score,answer_info')->find();
	    		$aInfo1 = unserialize($aInfo['answer_info']);
	    		$answerInfo = array();
	    		foreach ($aInfo1 as $k => $v) {
	    			$answerInfo[$v['title_id']] = $v['choice'];
	    		}
	    		$result['score'] = $aInfo['score'];
	    		if (count($pinfo) > 0) {
	    			$result['paper_info'] = $pinfo;
	    			//试卷的题目信息
	    			$subinfo = $sub->where(['paper_id' => $paperId,'status' => 1])->select();
	    			if (count($subinfo) > 0) {
		    			foreach ($subinfo as $k => $v) {
		    				$con['paper_id'] = $paperId;
		    				$con['subject_id'] = $v['id'];
		    				$con['status'] = 1;
		    				$finfo = $field->where($con)->field('id,paper_id,subject_id,title,correct')->select();
		    				$subinfo[$k]['field'] = $finfo;
		    				$subinfo[$k]['choose'] = $answerInfo[$v['id']];
		    			}
	    			$result['subject_info'] = $subinfo;
	    			}
	    		}
	    			
	    		$this->assign("info",$result);
	    		$this->display();
	    	} catch (\Exception $e) {
	    		throw $e;
	    	}
    	}
    }
    
    
    
}



















